import { Component, OnInit } from '@angular/core';
import { PaginationModel } from "../../../../widgets/pagination/pagination.component";
import { PubProductPageService } from "./pub-product-page.service";
import { LayoutComponent } from './../../../global/layout/layout.component';
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router';
import { environment } from '../../../../../environments/environment';
import { DataService } from '../../../../services/data.service';
import * as _ from 'lodash';

@Component({
  selector: 'app-pub-product-page',
  templateUrl: './pub-product-page.component.html',
  styleUrls: ['./pub-product-page.component.scss'],
  providers: [PubProductPageService]
})
export class PubProductPageComponent implements OnInit {
  // 菜单数据
  public menuData: any;
  // 菜单按钮集合
  public menuResourceSet: any;
  // 列表是否全选
  public gmisCheckedAll = false;
  // 公募列表数据
  public gmDataList;
  // 公募分页列表post数据
  public gmpostBody = {
    pageIndex: 1,
    pageSize: 10,
    sFundStatus: 0,
    sFullName: '',
    sFundCode: '',
    sIsRegisterChg: ''
  }
  // 公募列表分页配置
  public gmListPageInfo = {
    currentPageNum: 1,
    pageSize: 10,
    totalPages: 1,
    total: 0,
    pagesShow: 5,
    startRow: 0,
    endRow: 0,
    pageList: [5, 10, 25, 50, 100]
  }
  constructor(
    public pubProductPageService: PubProductPageService,
    public router: Router,
    public layoutComponent: LayoutComponent,
    public dataService: DataService
  ) { }

  ngOnInit() {
    let that = this;
    // 权限处理
    let SessionMenuData = sessionStorage.getItem('menu');
    this.menuData = JSON.parse(SessionMenuData);
    this.dataService.initTree.subscribe((data) => {
      let SessionMenuData = sessionStorage.getItem('menu');
      this.menuData = JSON.parse(SessionMenuData);
    })
    this.gmList();
  }
  // 列表全选(所有列表公用)
  checkedAll() {
    if (this.gmisCheckedAll) {
      _.forEach(this.gmDataList, item => {
        item.isChecked = true;
      });
    } else {
      _.forEach(this.gmDataList, item => {
        item.isChecked = false;
      });
    }
  }
  // 列表是否全选(所有列表公用)
  ctrlCheckAll() {
    let that = this;
    let checkList = [];
    _.forEach(this.gmDataList, item => {
      if (item.isChecked) {
        checkList.push(item);
      }
    })
    if (checkList.length == this.gmDataList.length) {
      this.gmisCheckedAll = true;
    } else {
      this.gmisCheckedAll = false;
    }
  }
  // 公募列表
  gmList(pageNum?, pageSize?) {
    let that = this;
    this.gmisCheckedAll = false;
    this.gmpostBody.sFullName = _.trim(this.gmpostBody.sFullName);
    this.gmpostBody.sFundCode = _.trim(this.gmpostBody.sFundCode);
    this.gmpostBody.pageIndex = pageNum ? pageNum : this.gmListPageInfo.currentPageNum;
    this.gmpostBody.pageSize = pageSize ? pageSize : this.gmListPageInfo.pageSize;
    this.pubProductPageService.gmList(this.gmpostBody).subscribe(data => {
      if (data) {
        that.gmDataList = data.list;
        that.gmListPageInfo.currentPageNum = data.pageNum;
        that.gmListPageInfo.totalPages = data.pages;
        that.gmListPageInfo.total = data.total;
        that.gmListPageInfo.startRow = data.startRow;
        that.gmListPageInfo.endRow = data.endRow;
      }
    })
  }
  // 公募分页
  gmSizeChange(pageSize: number) {
    if (pageSize !== this.gmListPageInfo.pageSize) {
      this.gmListPageInfo.pageSize = pageSize;
      this.gmList();
    }
  }
  // 公募分页
  gmPageNavigation(currentPageNum: number) {
    this.gmListPageInfo.currentPageNum = currentPageNum;
    this.gmList(this.gmListPageInfo.currentPageNum, this.gmListPageInfo.pageSize);
  }
  // 公募导出
  gmExportExcel() {
    let that = this;
    let checkedItem = [];
    _.forEach(that.gmDataList, item => {
      if (item.isChecked == true) {
        checkedItem.push(item.nFundNo);
      }
    })
    let converItem = checkedItem.join(',');
    let getBody = {
      "nFundNoList": converItem,
      "sFundStatus": 0,
      "sFullName": that.gmpostBody.sFullName,
      "sFundCode": that.gmpostBody.sFundCode,
      "sIsRegisterChg": that.gmpostBody.sIsRegisterChg
    }
    const iframe = window['$']("<iframe id='downloadiframe'>");
    iframe.attr('style', 'display:none');
    //  转码，兼容ie
    getBody.sFullName = encodeURI(getBody.sFullName)
    iframe.attr('src', environment.server + '/pms/v1/publicissue/export/publicbaseList?token=' + sessionStorage.getItem('token') + '&nFundNoList=' + getBody.nFundNoList + '&sFundStatus=' + getBody.sFundStatus + '&sFullName=' + getBody.sFullName + '&sFundCode=' + getBody.sFundCode + '&sIsRegisterChg=' + getBody.sIsRegisterChg);
    window['$']('body').append(iframe);
    setTimeout("$('#downloadiframe').remove()", 60000);
  }
  // 公募edit
  gmEdit(item) {
    let that = this;
    // 获取按钮权限
    // this.getMenuResource('公募基金', item.sStatusZh, 'btn-pub');
    sessionStorage.setItem("currentTitle", "公募基金·详情");
    that.layoutComponent.nav(null, null, "公募基金·详情");
    that.router.navigate(["/pages/publicoffering/zh/detail/edit/" + item.nFundNo]);
    // let sStatusZh: any = '';
    // let sStatus: any = '';
    // switch (item.sStatus) {
    //   case 'fashjjlx':
    //     sStatusZh = '方案设计';
    //     sStatus = 'fashjjlx';
    //     break;
    //   case 'zhbbh':
    //     sStatusZh = '准备报会';
    //     sStatus = 'zhbbh';
    //     break;
    //   case 'ybhzsh':
    //     sStatusZh = '报会在审';
    //     sStatus = 'ybhzsh';
    //     break;
    //   case 'yhp':
    //     sStatusZh = '已获批';
    //     sStatus = 'yhp';
    //     break;
    //   case 'yfx':
    //     sStatusZh = '已发行';
    //     sStatus = 'yfx';
    //     break;
    // }
    // // 获取按钮权限
    // // this.getMenuResource('公募基金', item.sStatusZh, 'btn-pub');
    // sessionStorage.setItem("currentTitle", "详情·" + sStatusZh + "·公募");
    // that.layoutComponent.nav(null, null, "详情·" + sStatusZh + "·公募");
    // that.router.navigate(["/pages/publicoffering/" + sStatus + "/detail/edit/" + item.nFundNo]);
  }
  // 权限处理
  // Modular为模块名,status为产品状态,btn为按钮权限名称
  // 例如：一对一，未成立，btn-oto
  getMenuResource(Modular: any, status: any, btn: any) {
    let that = this;
    this.menuResourceSet = [];
    _.forEach(this.menuData, (item, index) => {
      if (item.sName == Modular) {
        _.forEach(item.children, (it, idx) => {
          if (it.sName == status) {
            that.menuResourceSet = it.resourceSet
            sessionStorage.setItem(btn, that.menuResourceSet);
          }
        })
      }
    })
  }
  // 点击表格头排序
  orderData(colName) {
    let listDataAsc = _.orderBy(this.gmDataList, [colName], ["asc"]);
    if (JSON.stringify(this.gmDataList) == JSON.stringify(listDataAsc)) {
      listDataAsc = _.orderBy(this.gmDataList, [colName], ["desc"]);
    }
    this.gmDataList = listDataAsc;
  }
}
